package cn.lagou.controller;

import cn.lagou.pojo.Resume;
import cn.lagou.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    private ResumeService resumeService;

    @RequestMapping("/add")
    public String add() {
        System.out.println("resume..add()");
        return "add";
    }

    @GetMapping("/upd")
    public String upd(HttpSession session, Long id) {
        System.out.println("resume..findById()");
        Resume resume = resumeService.findById(id);
        session.setAttribute("resume",resume);
        return "upd";
    }

    @GetMapping("/findAll")
    public String findAll(HttpSession session) {
        System.out.println("resume..findAll()");
        return toList(session);
    }

    @PostMapping("/insert")
    public String insert(HttpSession session, Resume resume) {
        System.out.println("resume..insert()");
       resumeService.insert(resume);

        return toList(session);
    }

    @PostMapping("/update")
    public String update(HttpSession session, Resume resume) {
        System.out.println("resume..update()");
        resumeService.update(resume);
        return toList(session);
    }

    @GetMapping("/delete")
    public String delete(HttpSession session, Long id) {
        System.out.println("resume..delete()");
        resumeService.delete(id);
        return toList(session);
    }

    private String toList(HttpSession session){
        List<Resume> resumes = resumeService.findAll();
        session.setAttribute("resumes",resumes);
        return "list";
    }

}
